<script setup lang="ts">
import { getCreditsMap } from "@/api/zhang";
import { useRoute, useRouter } from "vue-router";
import { ref, computed } from "vue";

const route = useRoute();
const router = useRouter();
// 定义响应式数据
const crew = ref({
  creditsMap: {}, // 初始化为空对象
  credits: {}, // 初始化为空对象
});

const loading = ref(false); // 定义 loading 状态

// 获取数据并更新 crew
getCreditsMap(route.query.movieId).then((res) => {
  crew.value = res.data; // 动态更新数据
  //   console.log(crew.value);
  loading.value = true; // 设置 loading 为 true
});

// 定义 actor 函数
const actor = (crew, key) => {
  router.push({
    path: "/actor",
    query: {
      movieId: crew.movieId,
      castId: key,
    },
  });
};

// 计算属性：过滤后的 creditsMap
const filteredCredits = computed(() => {
  const { creditsMap, credits } = crew.value;
  if (!creditsMap || !credits) return {};

  return Object.keys(credits)
    .filter((key) => creditsMap[key]) // 确保 creditsMap 中存在 key
    .reduce((result, key) => {
      result[key] = creditsMap[key];
      return result;
    }, {});
});

// 定义显示/隐藏小框的状态
const showPopup = ref(false);

// 切换小框显示状态
const togglePopup = () => {
  showPopup.value = !showPopup.value;
};

// 定义返回首页的函数
const fanhui = () => {
  router.push({
    path: "/",
  });
};
</script>

<template>
  <div class="crew" v-if="loading">
    <div class="a">
      <van-nav-bar :title="crew.$title" left-arrow @click-left="router.back()" />
      <van-icon name="wap-nav" @click="togglePopup" />
    </div>
    <div @click="fanhui" v-if="showPopup" class="popup">
    <p>首页</p>
    </div>
    <div class="b">
      <div
        @click="actor(crew, key)"
        class="c"
        v-for="(label, key) in filteredCredits"
        :key="key"
      >
        <p>
          {{ label }}<span>({{ crew.credits[key] }})</span>
        </p>
        <van-icon name="arrow" />
      </div>
    </div>

    <footer>
      <div class="d">
        <p><span>猫眼用户服务协议</span> I 客服电话：<span>1234-4321</span></p>
        <p>京ICP备123456号-1 京ICP备123456789123号-1</p>
        <p>广东猫眼文化传媒有限公司</p>
      </div>
      <img
        src="https://obj.pipi.cn/festatic/asgard/resources/images/movie/bottom-logo.png"
        alt=""
      />
    </footer>
  </div>
</template>

<style lang="less" scoped>
/* 定义小框样式 */
.popup {
  position: absolute;
  top: 40rem;
  right: 35rem;
  width: 100rem;
  height: 40rem;
  line-height: 40rem;
  background-color: #ffffff;
  color: #000;
  text-align: center;
  line-height: 20rem;
  border: 1rem solid #ddd;
  border-radius: 4rem;
  box-shadow: 0 2rem 4rem rgba(0, 0, 0, 0.1);
  font-size: 12rem;
  z-index: 1000;
  p{
    line-height: 40rem;
  }
}
footer {
  margin-top: 20rem;
  width: 100%;
  height: 100rem;
  background-color: #f4f4f4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  .d {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  p {
    line-height: 20rem;
    font-size: 11rem;
    color: #a9b2b3;
    span {
      color: #4e759e;
    }
  }
  img {
    width: 78rem;
    height: 22rem;
  }
}
.b {
  background-color: #fff;

  .c {
    width: 90%;
    height: 50rem;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1rem solid #d6cdcd;
    p {
      font-size: 14rem;
      span {
        color: #999;
      }
    }
    .van-icon {
      font-size: 20rem;
      color: #999;
      line-height: 50rem;
    }
  }
}
.crew {
  background-color: #f4f4f4;
  height: 100vh;
}
:deep(.van-icon-wap-nav) {
  position: absolute;
  font-size: 25rem !important;
  right: 8rem;
  top: 9.5rem;
  z-index: 9999;
}
:deep(.van-badge__wrapper) {
  color: #fff;
  font-size: 20rem;
}
:deep(.van-nav-bar__title) {
  color: #fff;
}
:deep(.van-nav-bar__content) {
  background-color: #e54847;
}
p {
  margin: 0;
}
</style>
